﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WFL.Data;

namespace WFL.RBAC
{
    [Table("Permissions")]
    public class Permission
    {
        public int RoleId
        {
            get;
            set;
        }

        public int ResourseId
        {
            get;
            set;
        }

        public int Operations
        {
            get;
            set;
        }

        public int LimitResourceId
        {
            get;
            set;
        }

        public int LimitResourceItemId
        {
            get;
            set;
        }

        

        public void Union(Permission item)
        {
            if (item == null)
            {
                return;
            }
            if (item.ResourseId != this.ResourseId
                || item.LimitResourceId != this.LimitResourceId
                || item.LimitResourceItemId != this.LimitResourceItemId)
            {
                throw new ArgumentException("无法合并不同范畴资源的权限");
            }
            this.Operations |= item.Operations;
        }

        public bool Allowed(int operation)
        {
            return (this.Operations & operation) == operation;
        }
    }
}
